﻿using System.Collections.Generic;
using GpgCore.Products;

namespace GpgCore.Market
{
    public class Exchange
    {
        public ProductType TradeProduct;

        public double TotalProdCount;
        public double TotalMoneyCount;
        public double NominatedMoneyCount;
        public double NominatedProductCount;
        public double CurrentBuyPrice;
        public double CurrentSalePrice;
        public Dictionary<object, double> MoneyLots;
        public Dictionary<object, double> ProductLots;

        public Exchange(ProductType tradeProduct)
        {
            MoneyLots = new Dictionary<object, double>();
            ProductLots = new Dictionary<object, double>();
            TradeProduct = tradeProduct;
        }

        public void OpenPositionWithMoney(double money, object sender)
        {
            if (MoneyLots.ContainsKey(sender))
                MoneyLots[sender] += money;
            else
                MoneyLots.Add(sender, money);

            TotalMoneyCount += money;
            NominatedMoneyCount += money;
            CalcPrice();
        }
        public void OpenPositionWithProduct(double product, object sender)
        {
            if (ProductLots.ContainsKey(sender))
                ProductLots[sender] += product;
            else
                ProductLots.Add(sender, product);

            TotalProdCount += product;
            NominatedProductCount += product;
            CalcPrice();
        }
        public double GetMyProduct(object sender)
        {
            var income = GetMyProductIncome(sender);
            NominatedMoneyCount -= GetMyMoneyLot(sender);
            TotalProdCount -= income;
            MoneyLots.Remove(sender);
            CalcPrice();
            return income;
        }
        public double GetMyMoney(object sender)
        {
            var income = GetMyMoneyIncome(sender);
            NominatedProductCount -= GetMyProductLot(sender);
            TotalMoneyCount -= income;
            ProductLots.Remove(sender);
            CalcPrice();
            return income;
        }

        public double GetMyMoneyIncome(object sender)
        {
            return GetMyProductLot(sender)*CurrentSalePrice;
        }
        public double GetMyProductIncome(object sender)
        {
            return CurrentBuyPrice != 0 ? GetMyMoneyLot(sender)/CurrentBuyPrice : 0;
        }

        public double GetMyMoneyLot(object sender)
        {
            return MoneyLots.ContainsKey(sender) ? MoneyLots[sender] : 0;
        }
        public double GetMyProductLot(object sender)
        {
            return ProductLots.ContainsKey(sender) ? ProductLots[sender] : 0;
        }

        private void CalcPrice()
        {
            CurrentBuyPrice = TotalProdCount !=  0
                ? NominatedMoneyCount/TotalProdCount
                : 0;
            CurrentSalePrice = NominatedProductCount !=  0
                ? TotalMoneyCount/NominatedProductCount
                : 0;
        }
    }
}
